package com.listeningframework.boot.codegen.utils;

import org.apache.commons.lang3.StringUtils;

import java.io.File;
import java.util.StringTokenizer;

/**
 * CodeGenUtils
 */
public class CodeGenUtils {

    private CodeGenUtils() {

    }

    /**
     * 生成Get方法名称
     *
     * @param fieldName 属性名称
     * @return String
     */
    public static String generateGetMethodName(String fieldName) {
        return "get" + StringUtils.capitalize(fieldName);
    }

    /**
     * 生成Set方法名称
     *
     * @param fieldName 属性名称
     * @return String
     */
    public static String generateSetMethodName(String fieldName) {
        return "set" + StringUtils.capitalize(fieldName);
    }

    /**
     * 包名转路径
     *
     * @param packageName 包名
     * @return String
     */
    public static final String package2Path(String packageName) {
        return packageName.replace(".", File.separator);
    }

    /**
     * 处理路径
     *
     * @param path 路径
     * @return String
     */
    public static final String separator(String path) {
        return path.replace("/", File.separator);
    }

    /**
     * Column Field To Java
     *
     * @param fieldName 字段名
     * @return String
     */
    public static String columnFieldToJava(String fieldName) {
        if (StringUtils.isBlank(fieldName)) {
            return null;
        }
        StringTokenizer tokenizer = new StringTokenizer(fieldName, "_");
        int index = 0;
        StringBuilder result = new StringBuilder();
        while (tokenizer.hasMoreTokens()) {
            String value = tokenizer.nextToken();
            if (index == 0) {
                result.append(value.toLowerCase());
            } else {
                result.append(StringUtils.capitalize(value.toLowerCase()));
            }
            index++;
        }
        return result.toString();
    }

    /**
     * 是否为指定类型
     *
     * @param sourceClz  源
     * @param compareClz 目标
     * @return boolean
     */
    public static boolean isType(Class<?> sourceClz, Class<?> compareClz) {
        if (sourceClz == null || compareClz == null) {
            return false;
        }
        return StringUtils.equals(sourceClz.getName(), compareClz.getName());
    }

    public static String getValue(String value) {
        return StringUtils.isBlank(value) ? "" : value.trim();
    }

    public static final String treatColumnType(String value) {
        if (StringUtils.isBlank(value)) {
            return null;
        }
        return value.replaceAll("\\(.*\\)", "").trim().toUpperCase();
    }

    /**
     * 拼接子包
     *
     * @param subpackage 子包
     * @return
     */
    public static String getPadSubPackage(String subpackage) {
        return StringUtils.isBlank(subpackage) ? "" : ("." + subpackage);
    }

    public static String rpad(String value, int totalLength, String padChar) {
        String tempValue = StringUtils.isBlank(value) ? "" : value.trim();
        Integer padLngth = totalLength - tempValue.length();
        if (padLngth <= 0) {
            return tempValue;
        }
        StringBuilder builder = new StringBuilder();
        builder.append(tempValue);
        for (int i = 0; i < padLngth; i++) {
            builder.append(padChar);
        }
        return builder.toString();
    }
}
